package com.lxblyw.service;

import java.util.ArrayList;
import java.util.List;

import com.lxblyw.dao.EmployeeDao;
import com.lxblyw.dao.MessageDao;
import com.lxblyw.dao.ProjectDao;
import com.lxblyw.entity.Employee;
import com.lxblyw.entity.Message;
import com.lxblyw.entity.MessageItem;
import com.lxblyw.entity.Project;
import com.lxblyw.util.DateUtil;
import com.lxblyw.util.PKUtil;

public class MessageService {

	private MessageDao msgDao = new MessageDao();
	private EmployeeDao emDao = new EmployeeDao();
	private ProjectDao projDao = new ProjectDao();
	public List<MessageItem> findByProjectId(String id) {
		List<Message> msgList = msgDao.findByProjectId(id);
		List<MessageItem> msgItemList = new ArrayList<MessageItem>();
		for(Message msg : msgList) {
			MessageItem msgItem = new MessageItem();
			Employee employee = emDao.findById(msg.getEmployeeid());
			Project project = projDao.findProjectById(msg.getProjectid());
			msgItem.setMsg(msg);
			msgItem.setEmployee(employee);
			msgItem.setProject(project);
			msgItemList.add(msgItem);
		}
		return msgItemList;
	}
	public void save(Message msg) {
		msgDao.save(msg);
	}
	public void sendMsg(String msgContent, String employeeid, String projectid) {
		Message msg = new Message();
		msg.setId(PKUtil.getPK());
		msg.setContent(msgContent);
		msg.setCreatetime(DateUtil.getNow());
		msg.setEmployeeid(employeeid);
		msg.setProjectid(projectid);
		
		new MessageService().save(msg);
	}
	

}
